package isquirrel.recommender;

import isquirrel.beans.Property;
import isquirrel.beans.Recommendation;
import isquirrel.beans.User;
import isquirrel.semantic.DBpedia;
import isquirrel.util.HibernateUtil;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DynamicRecommender extends Recommender {
	
	public static void updateRecommendations(User user) {
		updateRecommendations2(user);
	}

	public static void updateRecommendations1(User user) {
		//get new recommendations
		List<Property> frequentProperties = getFrequentProperties(user, 7);
		DBpedia dbpedia = new DBpedia();
		
		List<Property> subset = new ArrayList<Property>();
		
		Set<String> newUrls = new HashSet<String>();
		
		//get urls first with 1 property, then with 2 properties, 3 etc until it can't find anymore
		while (frequentProperties.size() > 0) {
			frequentProperties.remove(0);
			subset.clear();
			for (Property p : frequentProperties) {
				subset.add(p);
				//System.out.println("Getting urls using "+subset.size()+ " properties");
				Set<String> urls = dbpedia.getUrls(subset);
				if (urls.size() > 0) {
					newUrls.addAll(urls);
				} else {
					break;
				}
			}
		}
		
		/*for (int i = 1; i < frequentProperties.size(); i++) {
			subset.clear();
			subset.add(frequentProperties.get(i));
			newUrls.addAll(dbpedia.getUrls(subset));
		}*/
		updateUrls(user, newUrls, "dynamic");
	}
	
	public static void updateRecommendations2(User user) {
		//get new recommendations
		List<Property> frequentProperties = getFrequentProperties(user, 7);
		DBpedia dbpedia = new DBpedia();
		
		List<Property> subset = new ArrayList<Property>();
		
		Set<String> newUrls = new HashSet<String>();
		
		//get urls first with 1 property, then with 2 properties, 3 etc until it can't find anymore
		while (frequentProperties.size() > 0) {
			subset.clear();
			subset.addAll(frequentProperties);
			while (subset.size()>1) {
//				System.out.println("Getting urls using "+subset.size()+ " properties");
				Set<String> urls = dbpedia.getUrls(subset);
//				for (String uu : urls) {
//					System.out.println(uu);
//				}
				if (urls.size() > 4) {
					newUrls.addAll(urls);
					break;
				} else {
					//nothing
				}
				subset.remove(subset.size()-1);
			}
			frequentProperties.remove(0);
		}
		updateUrls(user, newUrls, "dynamic");
	}
	
	@SuppressWarnings("unchecked")
	public static List<Property> getFrequentProperties(User user, int maxHowMany) {
		List<Property> properties = HibernateUtil
		.getSession()
		.createQuery(
				"from Property where user_id = ? order by frequency desc")
		.setLong(0, user.getUserID())
		.setMaxResults(maxHowMany)
		.list();
		
		return properties;
	}
	
	@SuppressWarnings("unchecked")
	public static List<Property> getFrequentProperties(User user, int maxHowMany, Set<String> blacklist) {
		String excluding = "";
		if ( blacklist != null &&  blacklist.size() > 0) {
			for( String s : blacklist) {
				excluding += " and attribute <> '" + s+"'";
			} 
		}
		List<Property> properties = HibernateUtil
		.getSession()
		.createQuery(
				"from Property where user_id = ? " + excluding + " order by frequency desc")
		.setLong(0, user.getUserID())
		.setMaxResults(maxHowMany)
		.list();
		
		return properties;
	}
	
	public static List<Recommendation> getRecommendations(User user,
			boolean liked, int howMany, boolean getAbstract) throws UnsupportedEncodingException {
		return getRecommendations(user, liked, howMany, "dynamic", getAbstract);
	}
}
